package com.glasses.business.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.glasses.business.R;
import com.glasses.business.pay.com.ConstantKeys;
import com.glasses.business.pay.com.PayAgent;
import com.glasses.business.pay.com.PayListener;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    public static PayListener getPayListener() {
        return payListener;
    }

    public static void setPayListener(PayListener payListener) {
        WXPayEntryActivity.payListener = payListener;
    }

    public static PayListener payListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, ConstantKeys.WxPay.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.app_tip);
            builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
            builder.show();
//            if (payListener != null) {
//                payListener.onPaySuccess(PayAgent.PayType.WECHATPAY);
//            }
        } else {
            if (payListener != null) {
                payListener.onPayFail(PayAgent.PayType.WECHATPAY, PayListener.ERROR_OTHER, "");
            }
        }
        // 防止立即关闭黑屏
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 200);

    }
}